<template>
	<view class="new_word_box">
		<block>
			<view class="title">
				<checkbox-group @change="selectAll" v-if="title !== '查看词表'">
					<label class="flex_ac">
						<checkbox color="#616bfa" :checked="isAllChecked" style="transform:scale(0.7)"></checkbox>
						<text>共{{ total }}个单词</text>
					</label>
				</checkbox-group>
			</view>
			<view class="title" v-if="title === '查看词表'">共{{ total }}个单词</view>
		</block>
		
		<view class="box" :style="{'height': title !== '查看词表' ? `calc(100% - 216rpx - env(safe-area-inset-bottom))` : `calc(100% - 84rpx)`}">
			<scroll-view scroll-y @scrolltolower="scrolltolower" style="height: 100%;">
				<view style="padding: 30rpx;" v-if="total > 0">
					<view v-if="title === '查看词表'">
						<view class="item flex_ac_js" v-for="item in words" :key="item.id" v-if="title == '查看词表'">
							<view class="word">
								<view class="title1">
									<text class="text1">{{ item.word }}</text>
									<text class="text2">{{ item.phonetic }}</text>
								</view>
								<view class="title2">{{ item.annotation }}</view>
							</view>
							<view class="audio" @click="playAudio()">
								<uni-icons type="sound-filled" color="#5057f9" size="24"></uni-icons>
							</view>
						</view>
					</view>
					<checkbox-group @change="checkboxChange" v-if="title !== '查看词表'">
						<view class="item flex_ac_js" v-for="item in words" :key="item.id">
							<label class="flex_ac">
								<view class="word flex_ac">
									<checkbox :value="item.id" color="#616bfa" :checked="item.checked" style="transform:scale(0.8)" />
									<view style="margin-left: 10rpx;">
										<view class="title1">
											<text class="text1">{{ item.word }}</text>
											<text class="text2">{{ item.phonetic }}</text>
										</view>
										<view class="title2">{{ item.annotation }}</view>
									</view>
								</view>
							</label>
							<view class="audio" @click="playAudio()">
								<uni-icons type="sound-filled" color="#5057f9" size="24"></uni-icons>
							</view>
						</view>
						
					</checkbox-group>
					<uni-load-more status="statusMore" :content-text="contentText" ></uni-load-more>
				</view>
				<emptyState :msg="'暂无数据'" :padding-num="'80rpx 0'" :len="total"></emptyState>
			</scroll-view>
		</view>
		<view class="btn_box flex_ac" v-if="title !== '查看词表'">
			<uni-badge :text="checkCount" absolute="rightTop">
				<view class="left">已选</view>
			</uni-badge>
			<view class="btn" :style="{'opacity': checkCount === 0 ? '0.6' : '1'}" @tap="checkCount === 0 ? '' : onSubmit()">开始学习</view>
		</view>
	</view>
</template>

<script>
	import { userListWord,getHardList,getUserListWord } from '@/api/word/word.js'
	import { wordByBook,wordListByBookId } from '@/api/homes/wordBook/wordBook.js'
	import { setStudyWord } from '@/api/practices/wordChoice.js'
	const innerAudioContext = uni.createInnerAudioContext();
	export default {
		data() {
			return {
				params: {
					page: 1,
					size: 20,
					data: ''
				},
				title: '',
				words: [],
				total: 0,
				statusMore: 'more',
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				checkCount: 0,
				ids: [],
				type:'',
				isAllChecked:false,
				
			}
		},
		onLoad(options) {
			let type = Number(options.type)
			this.type = Number(options.type)
			
			this.title = type === 0 ? '查看词表' : type === 1 ? '生词本' : '难记词'
			console.log(type,this.title);
			uni.setNavigationBarTitle({
				title: this.title
			})
			
			if (type === 0) {
				this.params = {
					...this.params,
					data: options.id
				}
				this.getWordBookList()
			} else {
				this.params = {
					...this.params,
				}
				this.getWordList()
			}
		},
		methods: {
			playAudio(url) {
				innerAudioContext.src = url
				innerAudioContext.play()
			},
			async getWordList() {
				let that = this
				that.params = {
					...that.params
				}
				try {
					if(that.type ==2) {
						console.log("查询难词");
						const{code,data,total} = await getHardList(that.params)
						if (code === 200) {
							that.total = total
							if (that.params.size * that.params.page >= total) {
								that.statusMore = 'noMore'
								uni.stopPullDownRefresh()
							} else {
								that.params.page ++
								that.statusMore = 'more'
							}
							that.words = [ ...that.words, ...data ]
							// that.total = total
							// if (total > that.params.size * that.params.page) {
							// 	that.statusMore = 'more'
							// 	that.params.page ++
							// 	that.words.push(...data)
							// } else if (total < that.params.size * that.params.page) {
							// 	that.statusMore = 'noMore'
							// 	that.words = data
							// }
						}
					} else{
						console.log("查询生词");
						const { code, data, total } = await getUserListWord(that.params)
						if (code === 200) {
							console.log(data);
							that.total = total
							if (that.params.size * that.params.page >= total) {
								that.statusMore = 'noMore'
								uni.stopPullDownRefresh()
							} else {
								that.params.page ++
								that.statusMore = 'more'
							}
							that.words = [ ...that.words, ...data ]
							// that.total = total
							// if (total > that.params.size * that.params.page) {
							// 	that.statusMore = 'more'
							// 	that.params.page ++
							// 	that.words.push(...data)
							// } else if (total < that.params.size * that.params.page) {
							// 	that.statusMore = 'noMore'
							// 	that.words = data
							// }
						}
					}
					
				} catch (err) {
					that.statusMore = 'loading'
				}
			},
			async getWordBookList() {
				let that = this
				that.params = {
					...that.params
				}
				console.log('111111111111111111',that.title);
				try {
					// await wordListByBookId(2,that.params.page,that.params.size).then(res=>{
					// 	console.log("ddddddddddd",res);
						
					// 	that.words = res.data
					// 	console.log("data",that.words);
					// })
					
					
					const { code, data, total } = await wordByBook(that.params)
					if (code === 200) {
						that.total = total
						if (that.params.size * that.params.page >= total) {
							that.statusMore = 'noMore'
							uni.stopPullDownRefresh()
						} else {
							that.params.page ++
							that.statusMore = 'more'
						}
						that.words = [ ...that.words, ...data ]
						
						
						
						// console.log("词表",that.words);
						// if (total > that.params.size * that.params.page) {
						// 	that.statusMore = 'more'
						// 	that.params.page ++
						// 	console.log('words1', that.words.push(...data));
							
						// 	// that.words.push(...data)
						// } else if (total > that.words) {
						// 	console.log('words2', that.words.push(...data));
						// 	that.words.push(...data)
						// } else {
						// 	console.log('words3', that.words.push(...data));
						// 	that.statusMore = 'noMore'
						// 	that.words = data
						// }
					}
				} catch (err) {
					that.statusMore = 'loading'
				}
			},
			scrolltolower() {
				if (this.statusMore !== 'noMore') {
					if (this.title === '查看词表') {
						this.getWordBookList() 
					} else {
						this.getWordList()
					}
				}
			},
			selectAll() {
				this.isAllChecked = !this.isAllChecked
				this.ids = this.words.map(item => item.id)
				if (this.isAllChecked) {
					this.words.map(item => item.checked = true)
					this.checkCount = this.words.length
				} else {
					this.words.map(item => item.checked = false)
					this.checkCount = 0
				}
			},
			checkboxChange(e) {
				this.checkCount = e.detail.value.length;
				this.ids = e.detail.value
				
				const idArr = new Set(e.detail.value)
				this.words.forEach(item => {
					if(idArr.has(item.id)) {
						item.checked = true
					} else {
						item.checked = false
					}
				})
			},
			goToPage(url) {
				uni.navigateTo({
					url
				})
			},
			async onSubmit() {
				let that = this
				try {
					const { code,data } = await setStudyWord(this.ids)
					if (code === 200) {
						uni.setStorageSync('words', { type: 'wordChoice' })
						uni.setStorageSync('wordsInfoList',data)
						that.goToPage('/pages/homes/spelling/spelling')
					}
				} catch (err) {}
			},
		},
	}
</script>

<style lang="scss" scoped>
.new_word_box {
	height: 100vh;
	background: $uni-bg-color-page;
	overflow: hidden;
	
	.title {
		height: 84rpx;
		padding: 20rpx 30rpx;
		border-bottom: 2rpx solid #98a7be;
	}
	
	.box {
		.item {
			border-bottom: 1rpx solid #98a7be;
			margin-bottom: 10rpx;
			
			.word {
				.title1 {
					margin-bottom: 6rpx;
					
					.text1 {
						font-size: 32rpx;
						font-weight: 800;
					}
					
					.text2 {
						font-size: 24rpx;
						margin-left: 16rpx;
						color: $uni-text-color-grey;
					}
				}
				
				.title2 {
					color: $uni-text-color;
					font-size: 28rpx;
					margin-bottom: 20rpx;
				}
			}
		}
	}

	.btn_box {
		width: 100%;
		height: calc(env(safe-area-inset-bottom) + 132rpx);
		padding: 30rpx;
		position: fixed;
		left: 0;
		bottom: 0;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 30rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 30rpx);
		
		.left {
			color: #333;
		}
		
		.btn {
			width: 280rpx;
			height: 72rpx;
			margin: 0 auto;
			line-height: 72rpx;
			text-align: center;
			border-radius: 50rpx;
			font-size: 28rpx;
			background-color: #0000ffca;
			color: #fff;
		}
	}
}
</style>
